﻿using Inovout.Kuajing.Lottery.Models;
using Inovout.Repositories;
using NHibernate;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inovout.Kuajing.Lottery.Repositories
{
    public class RedPacketRepository : BaseRepository
    {
        static string conn = System.Configuration.ConfigurationSettings.AppSettings["sqlConnectionString"];
        // System.Web.Configuration.WebConfigurationManager.ConnectionStrings["sqlConnectionString"];

        private SqlConnection Conn = null;

        public RedPacketRepository()
        {

        }

        public bool UpdateRedPacket(RedPacket redPacket, Active active, string userCode, bool is_NewUser)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" update Import_RedPacket set Status= 0 where RedPacketId=@redpacketId;");
            if (is_NewUser)
            {
                sql.Append(" insert into Import_Click(UserCode,ActiveId,CreatedTime,ModifiedTime,AccessedTime)");
                sql.Append(" values(@userCode,@activeId,getdate(),getdate(),getdate())");
            }
            else
            {
                sql.Append(" update Import_Click set ClickCount=ClickCount+1 where UserCode=@userCode and ActiveId=@activeId;");
            }


            DbCommand command = new System.Data.SqlClient.SqlCommand(sql.ToString());

            SqlParameter param_packetId = new SqlParameter();
            param_packetId.ParameterName = "@redpacketId";
            param_packetId.SqlDbType = SqlDbType.Int;
            param_packetId.Direction = ParameterDirection.Input;
            param_packetId.Value = (int)redPacket.Id;
            command.Parameters.Add(param_packetId);


            SqlParameter param_userCode = new SqlParameter();
            param_userCode.ParameterName = "@userCode";
            param_userCode.SqlDbType = SqlDbType.NVarChar;
            param_userCode.Direction = ParameterDirection.Input;
            param_userCode.Value = userCode;
            command.Parameters.Add(param_userCode);

            SqlParameter param_ctiveId = new SqlParameter();
            param_ctiveId.ParameterName = "@activeId";
            param_ctiveId.SqlDbType = SqlDbType.Int;
            param_ctiveId.Direction = ParameterDirection.Input;
            param_ctiveId.Value = (int)active.Id;
            command.Parameters.Add(param_ctiveId);

            try
            {
                openConnection();
                command.Connection = Conn;
                int result = command.ExecuteNonQuery();
                return result == 2 ? true : false;

            }
            finally
            {
                closeConnection();
            }
        }


        public bool UpdateRedPacket(RedPacket redPacket, Active active)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" update Import_RedPacket set Status= 0 where RedPacketId=@redpacketId and  ActiveId=@activeId;");
           

            DbCommand command = new System.Data.SqlClient.SqlCommand(sql.ToString());

            SqlParameter param_packetId = new SqlParameter();
            param_packetId.ParameterName = "@redpacketId";
            param_packetId.SqlDbType = SqlDbType.Int;
            param_packetId.Direction = ParameterDirection.Input;
            param_packetId.Value = (int)redPacket.Id;
            command.Parameters.Add(param_packetId);


            SqlParameter param_ctiveId = new SqlParameter();
            param_ctiveId.ParameterName = "@activeId";
            param_ctiveId.SqlDbType = SqlDbType.Int;
            param_ctiveId.Direction = ParameterDirection.Input;
            param_ctiveId.Value = (int)active.Id;
            command.Parameters.Add(param_ctiveId);

            try
            {
                openConnection();
                command.Connection = Conn;
                int result = command.ExecuteNonQuery();
                return result == 1 ? true : false;

            }
            finally
            {
                closeConnection();
            }
        }


        public bool UpdateActive(Active active)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" update Import_Active set Enabled= 0 where ActiveId=@activeId;");

            DbCommand command = new System.Data.SqlClient.SqlCommand(sql.ToString());

            SqlParameter param_activeId = new SqlParameter();
            param_activeId.ParameterName = "@activeId";
            param_activeId.SqlDbType = SqlDbType.Int;
            param_activeId.Direction = ParameterDirection.Input;
            param_activeId.Value = (int)active.Id;
            command.Parameters.Add(param_activeId);

            try
            {
                openConnection();
                command.Connection = Conn;
                int result = command.ExecuteNonQuery();
                return result == 1 ? true : false;
            }
            finally
            {
                closeConnection();
            }


        }

        public bool SaveWinnersList(WinnersList winnersList)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" insert into Import_WinnersList(UserCode,UserName,RedPacketId) values(@usercode,@username,@redpacketId)");

            DbCommand command = new System.Data.SqlClient.SqlCommand(sql.ToString());

            SqlParameter param_usercode = new SqlParameter();
            param_usercode.ParameterName = "@usercode";
            param_usercode.SqlDbType = SqlDbType.NVarChar;
            param_usercode.Direction = ParameterDirection.Input;
            param_usercode.Value = winnersList.UserCode;
            command.Parameters.Add(param_usercode);

            SqlParameter param_username = new SqlParameter();
            param_username.ParameterName = "@username";
            param_username.SqlDbType = SqlDbType.NVarChar;
            param_username.Direction = ParameterDirection.Input;
            param_username.Value = string.IsNullOrEmpty(winnersList.UserName) ? "" : winnersList.UserName;
            command.Parameters.Add(param_username);


            SqlParameter param_prizeId = new SqlParameter();
            param_prizeId.ParameterName = "@redpacketId";
            param_prizeId.SqlDbType = SqlDbType.Int;
            param_prizeId.Direction = ParameterDirection.Input;
            param_prizeId.Value = (int)winnersList.RedPacket.Id;
            command.Parameters.Add(param_prizeId);

            try
            {
                openConnection();
                command.Connection = Conn;
                int result = command.ExecuteNonQuery();
                return result == 1 ? true : false;
            }
            finally
            {
                closeConnection();
            }
        }

        private void openConnection()
        {
            if (Conn == null)
            {
                Conn = new SqlConnection(conn);
                Conn.Open();
            }
            else if (Conn.State == System.Data.ConnectionState.Closed)
            {
                Conn.Open();
            }
            else if (Conn.State == System.Data.ConnectionState.Broken)
            {
                Conn.Close();
                Conn.Open();
            }
        }

        private void closeConnection()
        {
            if (Conn != null)
            {
                Conn.Close();
            }

        }
    }
}
